/**
 * Created by Administrator on 2016/10/21.
 * 在线列表功能需求：
 * 对于在线列表中已经存在的用户：点击修改样式、点击加载历史纪录、点击去点小红圈、点击X加到历史列表、
 * 对于新进入的在线用户：判断在线列表中是否有该用户、判断历史列表中是否有该用户、有消息进入小红圈就+1；
 *
 * 历史列表功能需求：
 * 点击修改样式、点击加载历史纪录、点击X去除该用户、点击发送加到在线列表
 */
$(document).ready(function () {
    init();
    onlineClick();
    enterSend('.send');
    getInformation();
    bDetail();
    addDetail();

});
//获取客服名
var username = '';
function setUsername(username) {
    username = username;
    showAdminMessage(username);
}
//显示客服头像和名字。
function  showAdminMessage(username) {
    var $kefu_img = $('<div class="kefu_img"><img src="img/del.png"/></div>');
    var $confirm = $('<div class="confirm">+</div>');
    var $adminName = $('<div class="custorm_ser"></div>').text(username);
    var $leftTitle = $('.lefttitle');
    $leftTitle.append($kefu_img);
    $leftTitle.append($confirm);
    $leftTitle.append($adminName);
}

//存放id
var useridList = [];
var historyuserid = [];

//对于页面的初始化操作
function init() {
    $('body').height($(window).height());
    $('.li1').css('border-bottom', '1px solid #00CCCC');
    if (navigator.userAgent.indexOf('Firefox') >= 0) {
        $('#bs_right').css('overflow', 'scroll');
    } else if (navigator.userAgent.indexOf('Opera') >= 0) {
        $('#bs_right').css('overflow', 'scroll');
    } else {
        $('#bs_right').css('overflow-y', 'auto');
    }

    $('#frmright').attr('height', $(window).height());
    $('body').css('height', $(window).height());

    //为了防止用户拖动窗口自适应大小
    $(window).resize(function () {

        $('body').height($(window).height());
        if (navigator.userAgent.indexOf('Firefox') >= 0) {
            $('#bs_right').css('overflow', 'scroll');
        } else if (navigator.userAgent.indexOf('Opera') >= 0) {
            $('#bs_right').css('overflow', 'scroll');
        } else {
            $('#bs_right').css('overflow-y', 'auto');
        }
    });

    $('.rightcontent').empty();
}

//在线,历史聊天点击切换事件
function onlineClick() {
    $('.online_img').find('li').click(function () {
        $(this).css('background', '#e8e8e6');
        $(this).css('border-bottom', '1px solid #00CCCC');
        $(this).siblings().css('border', 'none');
        $(this).siblings().css('background', '#f3f3f3');
    })
    $('.li2').click(function () {
        $('#rightcontent').css('display', 'block');
        $('.foot').css('display', 'block');
        $('.account').css('display', 'none');
        //切换到历史记录时，把在线用户的样式异常，以防干扰。。
        $('.count .now .his_box').removeClass('active');
        $('.count .now').css('display', 'none');
        $('.count .history').css('display', 'block');
        Ex();
    });
    $('.li1').click(function () {
        $('#rightcontent').css('display', 'block');
        $('.foot').css('display', 'block');
        $('.account').css('display', 'none');
        $('.count .history').css('display', 'none');
        $('.count .now').css('display', 'block');
        Ex();
    })
}

//websocket通讯
var messageid;
//websocket通讯
function connent() {

    var msg = document.getElementById('rightcontent');
    var wsServer = 'ws://119.29.152.155:8002/?from=computer&type=server&userid='+ username +'&source=wu';
    var ws = new WebSocket(wsServer);
    //监听连接打开
    ws.onopen = function (evt) {
        console.log('连接成功');
    }
    //监听连接关闭
    ws.onclose = function (event) {
        console.log('连接关闭');
        // alert('连接关闭');
        top.Dialog.alert("亲，您已下线！");
    }

    //监听连接错误信息
    ws.onerror = function (event, e) {
        console.log('Error occured:' + event.data);
    }

    //消息接收
    ws.onmessage = function (message) {
        var message = JSON.parse(message.data);
        if (message.type == "message") {
            messageid = message.userid;
            showReceiveMessage(message);
        } else if (message.type == "online") {
            offLine(message);
        }
    }

    //发送数据
    $('.send').click(function () {
        sendMessage($('.righttitle').find('.text_shodow').text());

    });


    function sendMessage(userid) {
        if (ws != null) {
            //处理div中的文本 换行回车都改为<br>
            var text = document.getElementById('text').innerText.replace(/[\r\n]/g, '<br>');
            document.getElementById('text').innerText = '';
            if (text == "") {
                alert("输入的字符串为空,不能发送");
            } else {
                //向服务器发送数据
                var server = {type: "server", userid: 123456789, message: text}
//                var data = '{"type":"server","userid":"'+userid+'","message":"'+text+'"}';
                ws.send(JSON.stringify(server));
                showMessage(text);
            }
        } else {
            alert('提示', '您已经掉线，无法发送消息!');
        }
    }

}

//客户离线和上线的判断
function offLine(message) {
    if (message.online == "0") {
        for (var i = 0; i < $('.now .text_shodow').length; i++) {
            if ($('.now .text_shodow').eq(i).text() == message.userid) {
                $('.now .text_shodow').eq(i).css('display', 'block');
                $('.now .text_shodow').eq(i).parent().parent().append($('.now .text_shodow').eq(i).parent());
            }
        }

    } else if (message.online == "1") {
        for (var i = 0; i < $('.now .text_shodow').length; i++) {
            if ($('.now .text_shodow').eq(i).text() == message.userid) {
                $('.now .text_shodow').eq(i).css('display', 'none');
                $('.now .text_shodow').eq(i).parent().parent().prepend($('.now .text_shodow').eq(i).parent());
            }
        }

    }
}

//消息接受显示在页面上
function showReceiveMessage(message) {
    $html = $('#rightcontent');
    var myDate = new Date();
    var time = myDate.getTime();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    if (h < 10) {
        h = '0' + h;
    }
    if (m < 10) {
        m = '0' + m;
    }
    if (s < 10) {
        s = '0' + s;
    }
    var id = '';
    //比较右侧用户id和接受到信息的用户id一致的话就显示接收信息
//    if($('.righttitle').find('.text_shodow').text() ==  message.userid){
    $html.append('<div class="add"><div class="time_name"><span>' + h + ":" + m + ":" + s + '&nbsp;' + '</span><span>' + message.username + '</span></div><div class="add_img"><img src=' + message.headImage + '/>' +
            '</div><div class="add_wenju">' + message.data
            + '</div></div>');
    var div = document.getElementById('rightcontent');
    div.scrollTop = div.scrollHeight;
//    }
}
//发送数据显示在页面上
function showMessage(message) {
    var myDate = new Date();
    var time = myDate.getTime();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    if (h < 10) {
        h = '0' + h;
    }
    if (m < 10) {
        m = '0' + m;
    }
    if (s < 10) {
        s = '0' + s;
    }
    $html = $('#rightcontent');
    $html.append('<div class="huifu_list"><div class="huifu_time_name"><span>' + h + ":" + m + ":" + s + '&nbsp;' + '</span><span>' + username + '</span></div><div class="huifu_list_wenzi">' + message +
            '</div><div class="huifu_list_img"><img src="http://s.thsi.cn/js/m/newhelp/images/robot.png"/>'
            + '</div></div>');
    var div = document.getElementById('rightcontent');
    div.scrollTop = div.scrollHeight;

}
//回车发送数据
function enterSend(origin) {

    $(document).keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            // 阻止冒泡
            if (event && event.stopPropagation) {
                event.stopPropagation();
            } else {
                event.cancelBubble = true;
            }

            $(origin).trigger("click");
        }
    })


}

//显示在线聊天用户列表
function getCurrentSession() {
    $.ajax({
        type: "get",
        url: 'index.php?r=chat-record/get-session-list',
        dataType: 'json',
        success: function (data) {
            if (data.code == 1) {
                getOnlineList(data.currentSessionList);
                getList(data.historySessionList);
            }
            ;
        }
    })
}
function getOnlineList(data) {
    var now = $('.now');
    $.each(data, function (i) {
        var $his_box = $('<div class="his_box"></div>');
        var $loge = $("<div class='logo'><img class='lmg' src='" + data[i].headImage + "'/></div>");
        var $text_num = $("<div class='text_num' style='display: none;'>" + data[i].messageNumber + "</div>");
        var $his_name = $("<div class='his_name'>" + data[i].username + "</div>");
        var $shadow = $("<div class='text_shodow' style='display: none;'>" + data[i].userid + "</div>")
        var $his_cancel = $("<div class='his_cancel'><img  src='img/del.png'/></div>");
        $his_box.append($loge);
        $his_box.append($text_num);
        $his_box.append($his_name);
        $his_box.append($his_cancel);
        $his_box.append($shadow);
        now.append($his_box);
        useridList.push(data[i].userid);
        if (data[i].messageNumber > 0) {
            $('.text_num').eq(i).css('display', 'block');
            $('.text_shodow').eq(i).parent('.his_box').click(function () {

                $.ajax({
                    type: "post",
                    url: "./index.php?site=chat-record/eliminateRedSpots",
                    dataType: "json",
                    data: {userid: $(this).children('.text_shodow').text()}
                })
            });
        }
        if (data[i].online == "0") {
            $('.now .text_shodow').eq(i).css('display', 'block');
            $('.now .text_shodow').eq(i).parent().parent().append($('.now .text_shodow').eq(i).parent());
        } else if (data[i].online == "1") {
            console.log(data[i].username + "在线！");
        }
    });


}

Unify();

function getList(data) {
    $.each(data, function (i) {
        var history = $('.history');
        var $his_box = $('<div class="his_box"></div>');
        var $loge = $("<div class='logo'><img class='lmg' src='" + data[i].headImage + "'/></div>");
        var $text_num = $("<div class='text_num' style='display: none;'>" + data[i].messageNumber + "</div>");
        var $his_name = $("<div class='his_name'>" + data[i].username + "</div>");
        var $shadow = $("<div class='text_shodow' style='display: none;'>" + data[i].userid + "</div>");
        var $his_cancel = $("<div class='his_cancel'><img  src='img/del.png' ' /></div>");
        $his_box.append($loge);
        $his_box.append($text_num);
        $his_box.append($his_name);
        $his_box.append($his_cancel);
        $his_box.append($shadow);
        history.append($his_box);
        historyuserid.push(data[i].userid);
        if (data[i].messageNumber > 0) {
            $('.text_num').eq(i).css('display', 'block');
        }
        if (data[i].online == "0") {
            $('.history .text_shodow').eq(i).css('display', 'block');
            $('.history .text_shodow').eq(i).parent().parent().append($('.history .text_shodow').eq(i).parent());
        } else if (data[i].online == "1") {
            console.log(data[i].username + "在线！");
        }
    });

}

//显示客户头像和姓名
function showUsesrMessage(src, name, ID) {
    var righttitle = $('.righttitle');
    righttitle.empty();
    var $kehu_img = $('<div class="kehu_img"><img src=' + src + '/></div>');
    var $custom_name = $("<div class='custom_name'>" + name + "</div>");
    var $detail = $('<button id="detail" type="button" onclick="change()">用户详情</button>');
    var $shadow = $("<div class='text_shodow' style='display: none;'>" + ID + "</div>");
    righttitle.append($kehu_img);
    righttitle.append($custom_name);
    righttitle.append($shadow);
    righttitle.append($detail);
}
//统一列表列表样式
function Unify() {
    $(".his_box").live("click", ".history", function (e) {
        e.preventDefault();
        $(this).children('.text_num').css('display', 'none');
        $(this).children('.text_num').text(0);
        $(this).toggleClass('active');
        $(this).siblings().removeClass('active');
        // var that = this;
        //更新右侧用户头像和名字
        var src = $(this).find('.lmg').attr('src');
        var name = $(this).children('.his_name').text();
        var ID = $(this).children('.text_shodow').text();
        showUsesrMessage(src, name, ID);
        //获取历史聊天纪录
        // getHistoryChat($(this).children('.text_shodow').text(),username);
        Ex();
        X();
//        console.log($(this).children('.logo').children('.lmg').attr('src'));
        if ($('.active').parent().attr('class') == "now") {
            $('.list_item').empty();
            showComment(0, ID);
            getHistoryChat($(this).children('.text_shodow').text(), "current", $(this).children('.logo').children('.lmg').attr('src'));
        } else if ($('.active').parent().attr('class') == "history") {
            showComment(0, ID);
            getHistoryChat($(this).children('.text_shodow').text(), "history", $(this).children('.logo').children('.lmg').attr('src'));
        }
    });
}

//置顶问题
function Ex() {
    if ($('.active').length > 0 && $('.righttitle .text_shodow').text() == $('.active').children('.text_shodow').text()) {
        var userid = $('.active').children('.text_shodow').text();
        $('#Send').unbind().bind('click', function (e) {
            e.preventDefault();
            e.stopPropagation();

            if ($('.active').parent().attr('class') == "now") {
                $('.now').prepend($('.active'));

            } else if ($('.active').parent().attr('class') == "history") {
                $.ajax({
                    //请求ajax  告诉服务器将用户移动至在线列表
                    type: "post",
                    url: "Chat/sessionSet",
                    dataType: 'json',
                    data: {userid: userid, type: 3}
                });
                $('.now').prepend($('.active'));
                $('.now').css('display', 'block');
                $('.history').css('display', 'none');
                $('.now').css('display', 'block');
                $('.li1').css('background', '#e8e8e6');
                $('.li1').css('border-bottom', '1px solid #00CCCC');
                $('.li2').css('border', 'none');
                $('.li2').css('background', '#f3f3f3');
                var index = historyuserid.indexOf(userid);
                historyuserid.splice(index, 1);
                useridList.push(userid);

            }
        })
    }
}


//点击X
function X() {
    $('.his_cancel').unbind().live('click', '.his_box', function (e) {
        e.preventDefault();
        e.stopPropagation();
        $(this).parent().removeClass('active');
        if ($(this).parent().parent().attr('class') == "now") {
            $.ajax({
                //告诉后台 将用户移动到历史纪录
                type: "post",
                url: "Chat/sessionSet",
                dataType: 'json',
                data: {userid: $(this).parent().children('.text_shodow').text(), type: 1}
            });
            $('.history').prepend($(this).parent());

            var index = useridList.indexOf($(this).parent().children('.text_shodow').text());
            useridList.splice(index, 1);
            historyuserid.push($(this).parent().children('.text_shodow').text());




        } else if ($(this).parent().parent().attr('class') == "history") {
            $.ajax({
                //告诉后台 该用户在历史列表被删除
                type: "post",
                url: "Chat/sessionSet",
                dataType: 'json',
                data: {userid: $(this).parent().children('.text_shodow').text(), type: 2}
            });
            $(this).parent().remove();
            //并且将用户的历史用户id从数组中去掉。
            for (var i = 0; i < historyuserid.length; i++) {
                if (historyuserid[i] == $(this).parent().children('.text_shodow').text()) {

                    var index = historyuserid.indexOf($(this).parent().children('.text_shodow').text());
                    historyuserid.splice(index, 1);

                }
            }
            $('.righttitle').empty();
            $('#rightcontent').empty();
        }
    });
}


//获取历史聊天记录
function getHistoryChat(sessionId, type, src) {
    //先清空右边聊天框
    $('#rightcontent').empty();
    $.ajax({
        type: "post",
        url: "Chat/getsession",
        data: {sessionId: sessionId, type: type},
        dataType: 'json',
        success: function (data) {
            if (data.code == 1) {
                for (var i = 0; i < data.res.length; i++) {
                    $html = $('#rightcontent');
                    if (data.res[i].userFlag == "0") {
                        $html.append('<div class="add"><div class="time_name"><span>' + data.res[i].time + '&nbsp;</span><span>' + data.res[i].snederName + '</span></div><div class="add_img"><img src=' + src + '/>' +
                                '</div><div class="add_wenju">' + data.res[i].message + '</div></div>');
                    } else if (data.res[i].userFlag == "1") {
                        $html.append('<div class="huifu_list"><div class="huifu_time_name"><span>' + data.res[i].time + '&nbsp;</span><span>' + data.res[i].snederName + '</span></div><div class="huifu_list_wenzi">' + data.res[i].message +
                                '</div><div class="huifu_list_img"><img src="' + data.res[i].headImage + '"/></div></div>');
                    }
                    var div = document.getElementById('rightcontent');
                    div.scrollTop = div.scrollHeight;
                }
                // $html.append('<div>以上是历史记录</div>')
            } else {
                console.log("请输入正确的id");
            }
        }
    })
}


//onmessage 监听 检查用户是否存在  如果存在在历史列表 那就移动到在线； 如果都不存在，那就是新客户。
function userIsExsit(message) {
    if (useridList.indexOf(message.userid) < 0 && historyuserid.indexOf(message.userid) < 0) {
        useridList.push(message.userid);
        var now = $('.now');
        var $his_box = $('<div class="his_box"></div>');
        var $loge = $("<div class='logo'><img class='lmg' src='" + message.headImage + "'/></div>");
        var $text_num = $("<div class='text_num' style='display: none'>0</div>");
        var $his_name = $("<div class='his_name'>" + message.username + "</div>");
        var $shadow = $("<div class='text_shodow' style='display: none;'>" + message.userid + "</div>");
        var $his_cancel = $("<div class='his_cancel'><img  src='img/del.png' ' /></div>");
        $his_box.append($loge);
        $his_box.append($text_num);
        $his_box.append($his_name);
        $his_box.append($his_cancel);
        $his_box.append($shadow);
        now.prepend($his_box);

    } else if (useridList.indexOf(message.userid) < 0 && historyuserid.indexOf(message.userid) > -1) {
        for (var i = 0; i < historyuserid.length; i++) {
            (function (i) {
                if ($('.history .text_shodow').eq(i).text() == message.userid) {
                    $('.history .text_shodow').eq(i).css('display', 'none');
                    var box = $('.history .text_shodow').eq(i).parent();
                    $('.now').prepend(box);
                    $.ajax({
                        //请求ajax  告诉服务器将用户移动至在线列表
                        type: "post",
                        url: "Chat/sessionSet",
                        dataType: 'json',
                        data: {userid: message.userid, type: 3}
                    });

                    var index = historyuserid.indexOf(message.userid);

                    historyuserid.splice(index, 1);
                    useridList.push(message.userid);

                }
            })(i);
        }
    }
}
//onmessage监听消息的增加
function styleChange(useridList, message) {

    for (var i = 0; i < useridList.length; i++) {
        //消息自加
        if ($('.text_shodow').eq(i).text() == message.userid) {
            // $('.text_shodow').eq(i).css('display','none');
            $('.text_shodow').eq(i).parent('.his_box').find('.text_num').text(parseInt($('.text_shodow').eq(i).parent('.his_box').find('.text_num').text()) + 1);
//            console.log(parseInt($('.text_shodow').eq(i).parent('.his_box').find('.text_num').text()));

            //如果消息条数小于等于0，则隐藏小红圈
            if ($('.text_num').eq(i).text() <= 0) {
                $('.text_num').eq(i).css('display', 'none');
            } else {
                $('.text_num').eq(i).css('display', 'block');
                $('.now').prepend($('.text_shodow').eq(i).parent('.his_box'));
            }
        }

        //如果在和某个用户在聊天 则不显示小红圈
        if ($('.text_shodow').eq(i).text() == $('.righttitle .text_shodow').text()) {
            $('.text_num').eq(i).css('display', 'none');
            $('.text_num').eq(i).text(0);
            $.ajax({
                type: "post",
                url: "Chat/eliminateRedSpots",
                dataType: "json",
                data: {userid: $('.text_shodow').eq(i).text()}
            });
        }
        //如果数字大于0；当点击的时候向后台发送请求。告知后台消息
        if (parseInt($('.text_shodow').eq(i).parent('.his_box').find('.text_num').text()) > 0) {
            $('.text_shodow').eq(i).parent('.his_box').click(function () {
                $.ajax({
                    type: "post",
                    url: "Chat/eliminateRedSpots",
                    dataType: "json",
                    data: {userid: $(this).children('.text_shodow').text()}
                })
            });
        }

    }
}


//用户详情页面
function change() {
    $('#rightcontent').css('display', 'none');
    $('.foot').css('display', 'none');
    $('.account').css('display', 'block');
    var userId = $('.active').children('.text_shodow').html();
}
function back() {
    $('#rightcontent').css('display', 'block');
    $('.foot').css('display', 'block');
    $('.account').css('display', 'none');
}
//备注
function bDetail() {
    $('.basic').click(function () {
        var userId = $('.active').children('.text_shodow').html();
        var index = $(this).index();
        $('.remark').css('display', 'block');
        $('.information').css('display', 'none');


    })
}

function showComment(index, userId) {
    $('.remark').css('display', 'block');
    $('.information').css('display', 'none');
    $('.remark').find('.add').css('display', 'block');
    $.ajax({
        type: 'post',
        url: 'Chat/getComment?userId=' + userId,
        dataType: 'json',
        success: function (data) {
            if (data.code == 0) {
                $('#ad').children('p').remove();
                $('#ad').append('<p style="font-size: 14px;width: 100px;padding-top: 5px;">暂无备注</p>');
                isTrue = false;
                $('.list-item').remove();
            } else {
                $('.add').children('p').remove();
                var html = $('.remark').find('.add');
                $('.remark').html(html);
                $.each(data.data, function (index, array) {
                    var $html = $('.remark');
                    $html.append('<div class="list-item">' + '<span class="userInfo">' + array["username"] + '&nbsp;' + '</span>' +
                            '<span class="update_time">' + array['update_time'] + '</span></br><div class="comment">' +
                            '<span id="con' + array['id'] + '">' + array['comment'] + '</span><button  id="edit' + array['id'] + '" class="editDetail">编辑</button>' +
                            '<button id="del' + array['id'] + '" class="deleteDetail">删除</button>' + '</div></div>');

                });

                addDetail();
            }
        }
    })
}

//删除备注
function deleteDetail() {
    $('.deleteDetail').live('click', '.add', function () {
        $('#delUser').css('display', 'block');

        var id = this.id.replace('del', '');

        $('#delconfirm').unbind('click');
        $('#delconfirm').click(function () {
            $("#del" + id).parent().parent().css('display', 'none');
            $.ajax({
                type: 'post',
                url: 'Chat/deleteComment',
                data: {"commentId": id},
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        $("#del" + id).parent().parent().remove();

                    }

                    if ($("#del" + id).parent().parent().length < 0) {
                        $('#ad').append('<p style="font-size: 14px;width: 100px;padding-top: 5px;">暂无备注</p>')
                    }
                },
                error: function () {
                    top.Dialog.alert("备注删除失败！");
                }
            });
            $('#delUser').css('display', 'none');
        });

        drag($('#delUser'));
    });
}

//修改备注
function editDetail() {
    $('.editDetail').live('click', '.add', function () {
        $('#addUser').css('display', 'block');
        $('#cancel').click(function () {
            $('#addUser').css('display', 'none');
        });
        $('#x').click(function () {
            $('#addUser').css('display', 'none');
        });
        $('#alert_p').text('编辑备注');

        var comment = $(this).parent('.comment').children('span').text();
        $('#content').val(comment);


        var that = this;
        $('#confirm').unbind('click');
        $('#confirm').click(function () {

            var commentId = ($(that).attr('id').replace(/[^0-9]+/g, ''));
            var content = $('#content').val();
            if (content != "") {
                $.ajax({
                    type: "post",
                    url: "Chat/editComment",
                    data: {"commenterId": username, "commentId": commentId, "comment": content},
                    dataType: 'json',
                    success: function (data) {
                        $(that).parent('.comment').children('span').text(data.data.comment);
                        $(that).parents('.list-item').children('.userInfo').html(data.data.commenter + "&nbsp;");
                        $(that).parents('.list-item').children('.update_time').text(data.data.update_time)
                        $('#content').val('');
//                    $('#addUser').css('display','none');
                    },
                    error: function () {
                        top.Dialog.alert("备注修改失败!");
                    }
                });
                $('#addUser').css('display', 'none');
            } else {
                top.Dialog.alert("亲，输入内容不能为空！");
            }
        });
        drag($('#addUser'));
    });
}

//增加备注
function addDetail() {
    $('.addComment').click(function () {
        $('#addUser').css('display', 'block');
        $('#alert_p').text('添加备注');
        $('#confirm').unbind('click');
        $('#content').val('');
        $("#confirm").click(function () {
            if ($('#content').val() != "") {
                var content = $('#content').val();
                $.ajax({
                    type: "post",
                    url: "Chat/addComment",
                    data: {"commenterId": username, "userId": $('.righttitle').find('.text_shodow').text(), "comment": content},
                    dataType: 'json',
                    success: function (data) {

                        $('#ad').children('p').remove();
                        $('#ad').after('<div class="list-item">' + '<span class="userInfo">' + username + '&nbsp;' + '</span>' +
                                '<span class="update_time">' + data.data.update_time + '</span></br><div class="comment">' +
                                '<span id="con' + data.data.commenter_id + '">' + data.data.comment + '</span><button  id="edit' + data.data.id + '" class="editDetail">编辑</button>' +
                                '<button id="del' + data.data.id + '"  class="deleteDetail">删除</button>' + '</div></div>');
                        $('#content').val('');
                    },
                    error: function () {
                        top.Dialog.alert("备注添加失败!");
                    }
                });
                $('#addUser').css('display', 'none');
            } else {
                top.Dialog.alert("亲，输入内容不能为空！");
            }
        });

        $('#cancel').click(function () {
            $('#addUser').css('display', 'none');
        });
        $('#x').click(function () {
            $('#addUser').css('display', 'none');
        });
        drag($('#addUser'));
    });
}

//执行备注信息
editDetail();
deleteDetail();

//基本信息模块
function getInformation() {
    $('.infor').click(function () {
        var userId = $('.active').children('.text_shodow').text();
        $.ajax({
            type: "post",
            url: 'Chat/getCustomerInfo?userId=' + userId,
            dataType: 'json',
            success: function (data) {
                if (data.code == 1) {
                    var html = "";
                    $('.information').html(html);
                    var data1 = JSON.stringify(data.data);
                    $.each(data, function (index, array) {
                        if (index == 'data') {
                            var $html = $('.information');
                            $html.append('<div class="item item1">' + '平台:' + array['source'] + '</div><div class="item item2">' + '手机型号:' + array['model'] +
                                    '</div><div class="item item3">' + '设备系统版本:' + array['sdknum'] +
                                    '</div><div class="item item4">' + '版本号:' + array['appver'] +
                                    '</div><div class="item item5">' + '内部版本号:' + array['innerver'] +
                                    '</div><div class="item item6">' + '应用程序:' + array['for'] +
                                    '</div><div class="item item7">' + '连接服务器:' + array['server'] +
                                    '</div><div class="item item8">' + '模块:' + array['modular'] +
                                    '</div><div class="item item9">' + '分类:' + array['category'] +
                                    '</div><div class="item item10">' + '添加券商:' + array['addBrokerage'] + '</div>');
                            return false;
                        }
                        ;

                    });
                }
                ;

            }
        })
        var index = $(this).index();
        $('.remark').css('display', 'none');
        $('.information').css('display', 'flex');
    })
}


$('.acontent').find('li').live('click', '.acontent ul', function (e) {
    e.preventDefault();
    $(this).css('background', '#D7D7D7');
    $(this).siblings().css('background', '#fff');
})


//鼠标拖动div用户详情修改框
function drag(ele) {
    ele.mousedown(function (event) {

        var offset = ele.offset();

        x1 = event.clientX - offset.left;

        y1 = event.clientY - offset.top;

        ele.mousemove(function (event) {

            ele.css("left", (event.clientX - x1) + "px");

            ele.css("top", (event.clientY - y1) + "px");

        });

        ele.mouseup(function (event) {

            ele.unbind("mousemove");

        });

    });
}